package com.iwunu.gateway.controller;

import com.iwunu.gateway.constant.ResultCodeEnum;
import com.iwunu.gateway.service.BluetoothDataService;
import com.iwunu.gateway.service.CmdIssuedMongodbService;
import com.iwunu.gateway.service.ProtocolDataService;
import com.iwunu.gateway.util.ResultUtil;
import com.iwunu.gateway.vo.CmdIssuedVo;
import com.iwunu.iot.common.constant.ClientOpenTypeEnum;
import com.iwunu.iot.common.util.Result;
import com.sun.javafx.collections.MappingChange.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author kinfeng
 * @date 2020/4/28 11:22 AM
 * @description
 */
@CrossOrigin
@RequestMapping("/bluetoothServer")
@Controller
@Api(tags = "组装蓝牙协议数据", description = "组装蓝牙协议数据")
public class BluetoothCmdServerController {
    @Autowired
    private BluetoothDataService bluetoothDataService;
    @Autowired
    private ProtocolDataService protocolDataService;
    @Autowired
    private CmdIssuedMongodbService cmdIssuedMongodbService;
    @RequestMapping(value = "selectBindLockStatus",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "2.1查询绑定状态")
    public ResponseEntity selectBindLockStatus(){
        Result result = ResultUtil.generateResult(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.selectBindLockStatus());
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @RequestMapping(value = "selectPower",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "2.2查询电量状态")
    public ResponseEntity selectPower(){
        Result result = ResultUtil.generateResult(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.selectPower());
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @RequestMapping(value = "unBindLock",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "2.5解绑")
    public ResponseEntity unBindLock(
            @ApiParam(value = "baseCode 八位数字绑定时候生成的", required = true)@RequestParam String baseCode
    ){
        Result result = ResultUtil.generateResult(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.unBindLock(baseCode));
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @RequestMapping(value = "bindLock",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "2.6绑定")
    public ResponseEntity bindLock(
            @ApiParam(value = "baseCode 八位数字绑定时候生成的，前端生成，调用绑定接口的时候传到后台", required = true)@RequestParam String baseCode
    ){
        Result result = ResultUtil.generateResult(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.bindLock(baseCode));
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

	@RequestMapping(value = "/addCard/{clientId}",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "4增加卡")
    public ResponseEntity addCard(
    		 @ApiParam(value = "clientId", required = true)@PathVariable String clientId,
            @ApiParam(value = "startTime", required = true)@RequestParam String startTime,
            @ApiParam(value = "endTime", required = true)@RequestParam String endTime
    ){
    	Result result = null ;
    	CmdIssuedVo cmdVo = new CmdIssuedVo();
    	cmdVo.setEndTime(endTime);
    	cmdVo.setStartTime(startTime);
    	Integer pitNum = cmdIssuedMongodbService.saveBleSendImageData(cmdVo, clientId, ClientOpenTypeEnum.CARD.getType());
    	if(pitNum ==0) {
    		 result = new Result(ResultCodeEnum.PIT_FULL.getCode(), ResultCodeEnum.PIT_FULL.getDesc(),null);
        	 return new ResponseEntity<>(result, HttpStatus.OK);
    	}
        result = bluetoothDataService.addCard(pitNum,startTime,endTime);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/addFinger/{clientId}",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "4.8添加指纹")
    public ResponseEntity updatePwd(
    		 @ApiParam(value = "clientId", required = true)@PathVariable String clientId ,
            @ApiParam(value = "startTime", required = true)@RequestParam String startTime,
            @ApiParam(value = "endTime", required = true)@RequestParam String endTime
    ){
    	Result result = null ;
    	CmdIssuedVo cmdVo = new CmdIssuedVo();
    	cmdVo.setEndTime(endTime);
    	cmdVo.setStartTime(startTime);
    	Integer pitNum = cmdIssuedMongodbService.saveBleSendImageData(cmdVo, clientId, ClientOpenTypeEnum.FINGER.getType());
    	if(pitNum ==0) {
    		 result = new Result(ResultCodeEnum.PIT_FULL.getCode(), ResultCodeEnum.PIT_FULL.getDesc(),null);
        	 return new ResponseEntity<>(result, HttpStatus.OK);
    	}
    	result = bluetoothDataService.addNewFingerprint(pitNum,startTime,endTime);
        return new ResponseEntity<>(result, HttpStatus.OK);
    } 
    /**
     ** 删除卡 
     * @param Authorization
     * @param baseCode
     * @param cardNo
     * @param pitNum
     * @return
     */
    @RequestMapping(value = "/delCardNo/{cardNo}",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "4.1删除卡")
    public ResponseEntity delCardNo(
            @ApiParam(value = "baseCode", required = true)@RequestParam Integer baseCode,
            @ApiParam(value = "cardNo", required = true)@PathVariable("cardNo") String cardNo,
            @ApiParam(value = "pitNum", required = true)@RequestHeader Integer pitNum
            
    ){
        Result result =bluetoothDataService.delCardNo(baseCode,cardNo,pitNum);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
    /**
     ** 增加密码6位
     * @param Authorization
     * @param baseCode
     * @param pwd
     * @param type
     * @param lockId
     * @return
     */
    @RequestMapping(value = "/addPwd",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "4.4增加密码6位")
    public ResponseEntity addPwd(
            @ApiParam(value = "baseCode", required = true)@RequestParam Integer baseCode, 
            @ApiParam(value = "pitNum", required = true)@RequestParam Integer pitNum,
            @ApiParam(value = "pwd", required = true)@RequestHeader Integer pwd
    ){
        Result result =bluetoothDataService.addBluetoothPwd(baseCode,pitNum,pwd);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
    /**
     ** 增加密码6位临时密码
     * @param Authorization
     * @param baseCode
     * @param pwd
     * @param startTime
     * @param endTime
     * @param type
     * @param lockId
     * @return
     */
    @RequestMapping(value = "/addTempPwd",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "4.4.1增加密码6位")
    public ResponseEntity addTempPwd(
    		@ApiParam(value = "baseCode", required = true)@RequestParam Integer baseCode,
            @ApiParam(value = "pwd", required = true)@RequestParam Integer pwd,
            @ApiParam(value = "开始时间", required = true)@RequestParam String startTime,
            @ApiParam(value = "结束时间", required = true)@RequestParam String endTime,
            @ApiParam(value = "pitNum", required = true)@RequestParam Integer pitNum
    ){
        Result result =bluetoothDataService.addTempPwd(baseCode,pitNum,pwd,startTime,endTime);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/delPwd",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "4.5删除密码6位")
    public ResponseEntity delPwd(
            @ApiParam(value = "baseCode", required = true)@RequestParam Integer baseCode,
            @ApiParam(value = "pitNum坑号", required = true)@RequestParam Integer pitNum,
            @ApiParam(value = "pwd", required = true)@RequestHeader String pwd
            
    ){
        Result result = ResultUtil.generateResult(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),bluetoothDataService.delPwd(baseCode, pwd,pitNum));
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/updatePwd/{lockId}",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "4.7修改密码6位")
    public ResponseEntity updatePwd(
            @ApiParam(value = "登录获取的token值", required = true)@RequestHeader String Authorization,
            @ApiParam(value = "baseCode", required = true)@RequestParam Integer baseCode,
            @ApiParam(value = "pwd", required = true)@RequestHeader Integer pwd,
            @ApiParam(value = "oldpwd", required = true)@RequestHeader Integer oldpwd,
            @ApiParam(value = "锁类型U1/O1", required = true)@RequestParam String type,
            @ApiParam(value = "锁Id", required = true)@PathVariable("lockId") Long lockId,
            @ApiParam(value = "锁详情pitNum为空或NULL传NULL", required = true)@RequestHeader Integer pitNum
    ){
        Result result = ResultUtil.generateResult(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),bluetoothDataService.updateBluetoothPwd(baseCode,lockId,pwd,oldpwd,type,pitNum));
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
    
    
    
    @RequestMapping(value = "/delFinger",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "4.8.1删除指纹")
    public ResponseEntity delFinger(
            @ApiParam(value = "pitNum坑号", required = true)@RequestHeader Integer pitNum
            
    ){
        Result result =bluetoothDataService.delFinger(pitNum);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/syncTime",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "同步时间")
    public ResponseEntity syncTime(
            @ApiParam(value = "baseCode", required = true)@RequestParam String baseCode
    ){
        Result result = ResultUtil.generateResult(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.syncTime(baseCode));
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/setManagerPwd",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "3.0设置管理员密码")
    public ResponseEntity setManagerPwd(
            @ApiParam(value = "baseCode", required = true)@RequestParam Integer baseCode,
            @ApiParam(value = "mac地址", required = true)@RequestParam String mac,
            @ApiParam(value = "旧密码必须8位", required = true)@RequestHeader Integer oldpwd,
            @ApiParam(value = "密码必须8位", required = true)@RequestHeader Integer pwd
    ){
        Result result = bluetoothDataService.setManagerPwd(mac,baseCode,pwd,oldpwd);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/updateManagerPwd/{lockId}",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "3.1修改管理员密码")
    public ResponseEntity updateManagerPwd(
            @ApiParam(value = "登录获取的token值", required = true)@RequestHeader String Authorization,
            @ApiParam(value = "锁Id", required = true)@PathVariable("lockId") Long lockId,
            @ApiParam(value = "旧管理员密码", required = true)@RequestHeader Integer oldpwd,
            @ApiParam(value = "新管理员密码", required = true)@RequestHeader Integer newpwd
    ){
        Result result = bluetoothDataService.updateManagerPwd(lockId,oldpwd,newpwd);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
    
    @RequestMapping(value = "/openLockStep1",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "两个指令开锁第一步")
    public ResponseEntity openLockStep1(
            @ApiParam(value = "baseCode", required = true)@RequestParam String baseCode
    ){
        Result result = bluetoothDataService.openLockStep1(baseCode);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
    
    @RequestMapping(value = "/openLockStep2",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "两个指令开锁第二步")
    public ResponseEntity openLockStep2(
            @ApiParam(value = "baseCode", required = true)@RequestParam String baseCode,
            @ApiParam(value = "硬件返回随机数", required = true)@RequestParam String randomN,
            @ApiParam(value = "操作子类型1:开锁2：登录", required = true)@RequestParam String subType
    ){
        Result result = bluetoothDataService.openLockStep2(baseCode,randomN,"1");
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/loginStep1",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "登录第一步,用于每次连接蓝牙进行操作时使用除绑定/开门")
    public ResponseEntity loginStep1(
            @ApiParam(value = "baseCode", required = true)@RequestParam String baseCode
    ){
        Result result = bluetoothDataService.openLockStep1(baseCode);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
    
    @RequestMapping(value = "/loginStep2",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "登录第二步,用于每次连接蓝牙进行操作时使用除绑定/开门/设置管理原密码")
    public ResponseEntity loginStep2(
            @ApiParam(value = "baseCode", required = true)@RequestParam String baseCode,
            @ApiParam(value = "硬件返回随机数", required = true)@RequestParam String randomN
    ){
        Result result = bluetoothDataService.openLockStep2(baseCode,randomN,"2");
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
    /**
     * *读取开锁记录指令
     * @param Authorization
     * @return
     */
    @RequestMapping(value = "/records",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "读取开锁记录指令")
    public ResponseEntity records(){
        Result result = ResultUtil.generateResult(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.readLockEventCount());
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
    /**
     * 	导入记录,逐条导入
     * @param Authorization
     * @return
     */
    @RequestMapping(value = "/importRecord/{index}",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "导入记录,逐条导入")
    public ResponseEntity importRecord(
            @ApiParam(value = "index当前条数", required = true)@PathVariable("index") Integer index
    ){
        Result result = ResultUtil.generateResult(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.readLockEventAtIndex(index));
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
    
    /**
     ** 删除所有开锁记录
     * @param Authorization
     * @return
     */
    @RequestMapping(value = "/clearRecords",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "删除所有开锁记录")
    public ResponseEntity clearRecords(){
        Result result = ResultUtil.generateResult(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.clearLockEvents());
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
    /**
     * 	离线密码
     * @return
     */
    @RequestMapping(value = "/offonlinePwd",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取离线密码")
    public ResponseEntity offonlinePwd(
    		  @ApiParam(value = "baseCode", required = true)@RequestParam String baseCode,
              @ApiParam(value = "endTime", required = true)@RequestParam String endTime
    		){
        Result result = ResultUtil.generateResult(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.offonlinePwd(endTime,baseCode,1));
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
    
    /**
     * 	离线密码
     * @return
     */
    @RequestMapping(value = "/offonlinePwd/{flag}",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取离线密码")
    public ResponseEntity offonlinePwdMore(
    		  @ApiParam(value = "baseCode", required = true)@RequestParam String baseCode,
              @ApiParam(value = "endTime", required = true)@RequestParam String endTime,
              @ApiParam(value = "flag", required = true)@PathVariable Integer flag
    		){
    	if(flag==null || flag>1) {
    		flag = 1;
    	}
        Result result = ResultUtil.generateResult(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.offonlinePwd(endTime,baseCode,flag));
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
    
    /**
     ** 获取basecode
     * @param Authorization
     * @return
     */
    @RequestMapping(value = "/getBaseCode",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取basecode")
    public ResponseEntity getBaseCode(){
        Result result = ResultUtil.generateResult(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.getBaseCode());
        return new ResponseEntity<>(result, HttpStatus.OK);
    } 
    
   
    /**
     ** 配网connectNetwork
     * @param Authorization
     * @return
     */
    @RequestMapping(value = "/connectNetwork",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "配网connectNetwork")
    public ResponseEntity connectNetwork(    		  
    		@ApiParam(value = "ssid", required = true)@RequestParam String ssid,
            @ApiParam(value = "password", required = true)@RequestParam String password){
        Result result = ResultUtil.generateResult(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.connectNetwork(ssid.trim(),password.trim()));
        return new ResponseEntity<>(result, HttpStatus.OK);
    } 
}
